# Maintainer: Ranieri Althoff <ranisalt+aur@gmail.com>
# Contributor (MSYS2): Rafał Brzegowy <rafal.brzegfowy@yahoo.com>

_realname=mimalloc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.2.2
pkgrel=4
pkgdesc='General-purpose allocator with excellent performance characteristics (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/microsoft/mimalloc'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/microsoft/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-import-lib.patch"
        "0002-remove-the-lib-prefix-when-enabling-mimalloc-redirec.patch")
sha256sums=('0f9dc18b1b0b664e68dd57c537b9edc3e8c3b5e12932cff0d4277134318ee930'
            '23d4e33be33e542ba891c3b6c953c115c0a59e65e4f132ce90adb15df7695a3e'
            '24d056016a817d334e3edd296d589bfd989f3084635b24c7d8f01ccd3c23879c')

prepare() {
  cd ${_realname}-${pkgver}

  patch -Np1 -i ../0001-fix-import-lib.patch
  patch -Np1 -i ../0002-remove-the-lib-prefix-when-enabling-mimalloc-redirec.patch
}

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DMI_BUILD_OBJECT=OFF \
      -DMI_INSTALL_TOPLEVEL=ON \
      -DMI_BUILD_TESTS=OFF \
      -DMI_WIN_REDIRECT=OFF \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

check() {
  cmake -DMI_BUILD_TESTS=ON -S ${_realname}-${pkgver} -B build-${MSYSTEM}
  cmake --build build-${MSYSTEM}
  ctest build-${MSYSTEM} || true
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  install -Dm644 ${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
